package com.egg.basic.controller.web;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.egg.basic.entity.dict.DictType;
import com.egg.basic.model.request.DictTypePostOrPut;
import com.egg.basic.model.response.DictTypeInfo;
import com.egg.basic.model.response.DictTypePage;
import com.egg.basic.service.dict.IDictTypeService;
import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.response.PageData;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author east
 * @since 2022-06-08
 */
@Slf4j
@Api(tags = WebController.API_PREFIX + "字典类型表")
@RestController
@RequestMapping("/dict/type")
public class WebDictTypeController extends WebController {


    @Autowired
    private IDictTypeService dictTypeService;


    @ApiOperation(value = "字典类型表新增")
    @PostMapping
    public ResponseData<String> dictTypePOst(@RequestBody DictTypePostOrPut param) {
        Long id = dictTypeService.add(param);
        return ResponseData.success(id.toString());
    }

    @ApiOperation(value = "字典类型表分页")
    @GetMapping("/page")
    public ResponseData<PageData<DictTypePage>> dictTypePage(
            @ApiParam(value = "页数", required = true) @RequestParam(value = "current", defaultValue = "1") Integer current
            , @ApiParam(value = "页长", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            , @ApiParam(value = "关键字") @RequestParam(value = "keywords", required = false) String keywords
    ) {
        LambdaQueryWrapper<DictType> qw = dictTypeService.buildLambdaQw();
        qw.orderByDesc(DictType::getSort);
        if (ObjectUtil.isNotEmpty(keywords)) {
            qw.and(wrapper ->
                    wrapper.like(DictType::getName, keywords)
                            .or().like(DictType::getCode, keywords)
            );
        }
        Page<DictType> dbPage = dictTypeService.page(new Page<>(current, pageSize), qw);
        if (ObjectUtil.isEmpty(dbPage.getRecords())) {
            return ResponseData.success(new PageData<>(dbPage));
        }
        List<DictTypePage> list = JsonUtil.listToList(dbPage.getRecords(), DictTypePage.class);
        return ResponseData.success(new PageData<>(dbPage, list));
    }

    @ApiOperation(value = "字典类型表详情")
    @GetMapping("/{id}/info")
    public ResponseData<DictTypeInfo> dictTypeInfo(
            @ApiParam(value = "字典类型表主键ID", required = true) @PathVariable String id
    ) {
        DictType dbInfo = dictTypeService.baseGetById(id);
        DictTypeInfo objInfo = JsonUtil.objToObj(dbInfo, DictTypeInfo.class);
        return ResponseData.success(objInfo);
    }

    @ApiOperation(value = "字典类型表编辑")
    @PutMapping("/{id}")
    public ResponseData<String> dictTypePut(
            @ApiParam(value = "字典类型表主键ID", required = true) @PathVariable String id
            , @RequestBody DictTypePostOrPut edit
    ) {
        dictTypeService.edit(id, edit);
        return ResponseData.success(id);
    }

    @ApiOperation(value = "字典类型删除")
    @DeleteMapping
    public ResponseData<String> dictTypeDel(
            @ApiParam(value = "ID集合", required = true) @RequestParam(value = "ids") Collection<String> ids
    ) {
        dictTypeService.del(
                ids.stream().filter(o -> ObjectUtil.isNotEmpty(o)).collect(Collectors.toSet())
        );
        return ResponseData.success();
    }


}

